package c.francis.controller;

import java.util.Date;
import java.util.Random;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import c.francis.pojo.Category;
import c.francis.pojo.Subscription_dtl;
import c.francis.service.impl.CategoryServiceImpl;
import c.francis.service.impl.RoomServiceImpl;
import c.francis.service.impl.SubscriptionDtlServiceImpl;

@Controller
public class SubscriptionDtlController {
	@Autowired
	private SubscriptionDtlServiceImpl dtlServiceImpl;
	@Autowired
	private CategoryServiceImpl categotyserviceimpl;
	
	@Autowired
	private RoomServiceImpl roomserviceimpl;
	
	@RequestMapping("insertnewsubstl")
	public @ResponseBody int insertNewSubdtl(/**int sid,int rid,*/Integer	sid,Integer rid,
											 Date sdate,Date edate,int residetype) {
		double price = 0;
		//��ѯ�۸�
//		System.out.println("---------------rid="+rid);
//		System.out.println("---------------sid="+sid);

		/**
		 * rid实在是不知道是什么东西（不知道它的含义是什么），前端也不知道是从哪里获取的-->addbooks.js
		 */
		//int	rid= (int)(Math.random()*100);

		if (rid == null) {
			return 0;
		}

		Category category = categotyserviceimpl.queryCategoryByRid(rid);
		if(residetype==1) {
			price=category.getBedprice();
		}else if(residetype==2) {
			price=category.getRoomprice();
		}
		//����
		try {
			dtlServiceImpl.insertnewsubdtl(sid, rid, sdate, edate, residetype, price);
			//����֮���޸ķ���״̬Ϊ����Ԥ��״̬
			roomserviceimpl.updateStatus(rid, "c");
			return 1;
		} catch (Exception e) {
			return 0;
		}
	}
	
	
	@RequestMapping("deletesubdtl")
	public @ResponseBody int deletesubdtl(@RequestParam("id")int id) {
		Subscription_dtl subscription_dtl = dtlServiceImpl.queryById(id);
		int rid = subscription_dtl.getRid();
		try {
			//ɾ������
			dtlServiceImpl.deleteSubdtlBySidRid(id);
			//�ͷŷ���
			roomserviceimpl.updateStatus(rid, "o");
			return 1;
		} catch (Exception e) {
			return 0;
		}
	}
}
